/**
 * @(#)com.my.test.Main.java
 * 南京中兴软创科技有限责任公司
 * @date 2013-10-22
 */
package com.my.test.aop;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.my.test.aop.advice.GreetingBeforeAdvice;
import com.my.test.aop.impl.GreetingImpl;

/**
 * 
 * @author <a href="tong.hao@zte.com.cn">童浩</a>
 * @version 1.0
 * @since 2013-10-22 下午5:38:27
 */

public class Main {

	public static void main(String[] args) {
		test3();
	}

	public static void test() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml"); // 获取 Spring
		ProxyFactory proxyFactory = new ProxyFactory(); // 创建代理工厂

		proxyFactory.setTarget(new GreetingImpl()); // 射入目标类对象

		proxyFactory.addAdvice(new GreetingBeforeAdvice()); // 添加前置增强

		// 仅仅从容器中取
		Greeting greeting = (Greeting) context.getBean("greetingImpl");
		greeting.sayHello("Jack");

		// 取出代理
		Greeting greeting2 = (Greeting) proxyFactory.getProxy(); // 从代理工厂中获取代理
		greeting2.sayHello("Jack");
	}

	/**
	 * Spring AOP：引入增强
	 * GreetingImpl不想再去实现Apology接口,让spring来动态的为其实现接口,从而可以调用Apology接口的方法
	 */
	// 通过配置文件
	public static void test2() {

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");

		Greeting greeting = (Greeting) context.getBean("greetingProxy");
		greeting.sayHello("Jack");
		Apology apology = (Apology) greeting; // 强制转型为 Apology 接口
		apology.saySorry("Jack");

	}
	
	// 注解
	public static void test3() {
		
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		
		Greeting greeting = (Greeting) context.getBean("greetingImpl");
		greeting.sayHello("Jack");
		Apology apology = (Apology) greeting; // 强制转型为 Apology 接口
		apology.saySorry("Jack");
		
	}

}
